package com.xyazm.order.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.xyazm.common.constants.RocketMqConstant;
import com.xyazm.common.enums.RocketMqTopicEnum;
import com.xyazm.common.message.ActualRefundMessage;
import com.xyazm.inventory.domain.param.ReleaseProductStockParam;
import com.xyazm.order.domain.dto.AuditAcceptReleaseAssetsDTO;
import com.xyazm.order.domain.dto.ReleaseProductStockDTO;
import com.xyazm.order.mapstruct.OrderApiMapstruct;
import com.xyazm.order.mq.producer.DefaultProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 接收客服审核通过后的 监听 释放资产消息 ok
 * @author xuyou
 * @date 2022年07月30日 14:33
 */
@Slf4j
@Component
public class AuditAcceptReleaseAssetsListener implements MessageListenerConcurrently {

    @Autowired
    private DefaultProducer defaultProducer;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

        try {
            for (MessageExt messageExt : list) {
                // 1、消费到释放资产message
                String message = new String(messageExt.getBody());
                log.info("AuditPassReleaseAssetsListener message:{}", message);
                AuditAcceptReleaseAssetsDTO auditAcceptReleaseAssetsDTO = JSONObject.parseObject(message, AuditAcceptReleaseAssetsDTO.class);
                // 2、发送释放库存MQ
                ReleaseProductStockDTO releaseProductStockDTO = auditAcceptReleaseAssetsDTO.getReleaseProductStockDTO();
                ReleaseProductStockParam releaseProductStockParam = buildReleaseProductStockParam(releaseProductStockDTO);
                defaultProducer.sendMessage(RocketMqTopicEnum.RELEASE_INVENTORY.getTopic(),
                        JSONObject.toJSONString(releaseProductStockParam), "客服审核通过释放库存");

                // 3、发送实际退款
                ActualRefundMessage actualRefundMessage = auditAcceptReleaseAssetsDTO.getActualRefundMessage();
                defaultProducer.sendMessage(RocketMqTopicEnum.ACTUAL_REFUND.getTopic(),
                        JSONObject.toJSONString(actualRefundMessage), "客服审核通过实际退款");

            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            log.error("consumer error", e);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    }


    /**
     * 组装释放库存数据
     */
    private ReleaseProductStockParam buildReleaseProductStockParam(ReleaseProductStockDTO releaseProductStockDTO) {
        // 补充订单条目
        List<ReleaseProductStockParam.OrderItemParam> orderItemParamList =
                OrderApiMapstruct.INSTANCE.convertOrderItemParam2(releaseProductStockDTO.getOrderItemDTOList());
        ReleaseProductStockParam releaseProductStockRequest = new ReleaseProductStockParam();
        releaseProductStockRequest.setOrderId(releaseProductStockDTO.getOrderId());
        releaseProductStockRequest.setOrderItemParamList(orderItemParamList);
        return releaseProductStockRequest;
    }
}
